package com.yao.controller;

import com.yao.common.Result;
import com.yao.po.AddressBook;
import com.yao.service.IAddressBookService;
import com.yao.utils.UserContext;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/userAddress")
@RestController
public class UserAddress {

    private static final Logger log = LoggerFactory.getLogger(UserAddress.class);
    private final IAddressBookService addressBookService;

    public UserAddress(IAddressBookService addressBookService) {
        this.addressBookService = addressBookService;
    }

    /**
     * 新增用户地址
     *
     * @param addressBook 用户地址
     * @return Result
     */
    @PostMapping("/save")
    public Result<Object> addUserAddress(@RequestBody AddressBook addressBook) {
        log.info("新增用户地址...");
        addressBook.setOpenId(UserContext.getUser());
        boolean save = addressBookService.save(addressBook);
        if (save) {
            return Result.success("成功");
        } else {
            return Result.error("添加失败");
        }
    }

    /**
     * 获取用户所有地址
     *
     * @return Result
     */
    @GetMapping("/findUserAddress")
    public Result<Object> getUserAddress() {
        log.info("获取用户所有地址...");
        String openId = UserContext.getUser();

        List<AddressBook> list = addressBookService.lambdaQuery().eq(AddressBook::getOpenId, openId)
                .list();
        return Result.success(list);
    }

    /**
     * 获取用户某一个地址
     *
     * @param id 地址id
     * @return Result
     */
    @GetMapping("/{id}")
    public Result<Object> getAddressById(@PathVariable("id") String id) {
        log.info("获取用户某一个地址...");
        AddressBook addressBook = addressBookService.getById(id);
        return Result.success(addressBook);
    }

    /**
     * 更新用户地址
     *
     * @param addressBook 用户地址
     * @return Result
     */
    @PostMapping("/update")
    public Result<Object> updateAddressById(@RequestBody AddressBook addressBook) {
        log.info("更新用户地址...");
        Long id = addressBook.getId();
        boolean update = addressBookService.updateById(addressBook);
        if (update) {
            return Result.success("成功");
        } else {
            return Result.error("失败");
        }
    }

    /**
     * 删除用户地址
     *
     * @param id 地址id
     * @return Result
     */
    @GetMapping("/delete/{id}")
    public Result<Object> deleteAddressById(@PathVariable("id") String id) {
        log.info("删除用户地址...");
        boolean remove = addressBookService.removeById(id);
        if (remove) {
            return Result.success("成功");
        } else {
            return Result.error("失败");
        }
    }

    /**
     * 获取用户订单地址
     *
     * @return Result
     */
    @GetMapping("/getOrderAddress")
    public Result<Object> getOrderAddress() {
        log.info("获取用户默认地址...");
        String openId = UserContext.getUser();
        List<AddressBook> list1 = addressBookService.lambdaQuery().eq(AddressBook::getOpenId, openId)
                .eq(AddressBook::getIsDefault, true)
                .list();

        if (list1.size()!= 0) {
            return Result.success(list1.getFirst());
        } else {
            List<AddressBook> list = addressBookService.lambdaQuery().eq(AddressBook::getOpenId, openId).list();
            if (list.size() != 0) {
                return Result.success(list.getFirst());
            }
            return Result.success("没有地址");
        }
    }
}
